package com.sixkey.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sixkey.constant.CommonConstant;
import com.sixkey.entity.Room;
import com.sixkey.entity.vo.RoomDetailVo;
import com.sixkey.entity.vo.RoomVo;
import com.sixkey.exception.ServiceException;
import com.sixkey.mapper.RoomMapper;
import com.sixkey.response.ResponseEnum;
import com.sixkey.service.IRoomService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2023-10-01
 */
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements IRoomService {

    /**
     * 分页获取会议室数据
     * @param pageNo
     * @param pageSize
     * @param roomVo
     * @return
     */
    @Override
    public Page<Room> queryList(Integer pageNo, Integer pageSize, RoomVo roomVo) {
        Page<Room> pageInfo = new Page<>(pageNo,pageSize);
        LambdaQueryWrapper<Room> wrapper = new LambdaQueryWrapper<>();
        //进行模糊查询
        wrapper.like(!StringUtils.isEmpty(roomVo.getName()),Room::getName,roomVo.getName());
        wrapper.like(!StringUtils.isEmpty(roomVo.getLocation()),Room::getLocation,roomVo.getLocation());
        wrapper.like(!StringUtils.isEmpty(roomVo.getStatus()),Room::getStatus,roomVo.getStatus());
        //排序
        wrapper.orderByDesc(Room::getUpdateTime);
        Page<Room> roomPage = this.page(pageInfo, wrapper);
        List<Room> roomList = roomPage.getRecords();
        return roomPage;
    }

    /**
     * 删除会议室
     * @param id
     */
    @Override
    public void removeBatch(List<Integer> id) {
        List<Room> rooms = this.listByIds(id);
        //1、使用中状态的不能被删除
        List<Room> collect = rooms.stream().filter(room -> room.getStatus().equalsIgnoreCase(CommonConstant.ROOM_STATUS_USED)).collect(Collectors.toList());
        if(!CollectionUtils.isEmpty(collect)){
            throw new ServiceException(ResponseEnum.ROOM_USED);
        }
        //2、被预约中状态的不能被删除
        List<Room> roomList = rooms.stream().filter(room -> room.getStatus().equalsIgnoreCase(CommonConstant.ROOM_STATUS_RESERVATION)).collect(Collectors.toList());
        if(!CollectionUtils.isEmpty(roomList)){
            throw new ServiceException(ResponseEnum.ROOM_RESERVATION);
        }
        //3、删除
        baseMapper.deleteBatchIds(id);
    }

    /**
     * 小程序端首页获取会议室图片
     * @return
     */
    @Override
    public List<Map<String,Object>> getImage() {
        List<Room> roomList = this.list();
        List<Map<String,Object>> images = new ArrayList<>();
        roomList.stream().forEach(room -> {
            String imageUrl = room.getImage();
            Map<String, Object> map = new HashMap<>();
            map.put("id",room.getId());
            map.put("image",imageUrl);
            images.add(map);
        });
        return images;
    }


    /**
     * 后台预约管理获取会议室名称数据
     * @return
     */
    @Override
    public List<Map<String, Object>> getMeetRoomNames() {
        //查询会议室处于未预约状态
        List<Room> roomList = this.list(new QueryWrapper<Room>().eq("status",CommonConstant.ROOM_STATUS_NOT_RESERVATION));
        List<Map<String,Object>> roomNames = new ArrayList<>();
        //封装成前端所需要的格式
        roomList.stream().forEach(room -> {
            Map<String,Object> map = new HashMap<>(roomList.size());
            map.put("value",room.getName());
            map.put("label",room.getName());
            roomNames.add(map);
        });
        return roomNames;
    }

    /**
     * 小程序端分页获取会议室数据
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public Map<String,Object> apiQueryList(Integer pageNo, Integer pageSize,String roomName) {
        Page<Room> pageInfo = new Page<>(pageNo,pageSize);
        LambdaQueryWrapper<Room> wrapper = new LambdaQueryWrapper<>();
        //进行模糊查询
        wrapper.like(!StringUtils.isEmpty(roomName),Room::getName,roomName);
        //排序
        wrapper.orderByDesc(Room::getUpdateTime);
        Page<Room> roomPage = this.page(pageInfo, wrapper);
        List<Room> roomList = roomPage.getRecords();
        List<Map<String, Object>> list = new ArrayList<>();
        roomList.stream().forEach(room -> {
            Map<String, Object> map = new HashMap<>(roomList.size());
            map.put("id",room.getId());
            map.put("name",room.getName());
            map.put("image",room.getImage());
            map.put("capacity",room.getCapacity());
            map.put("status",room.getStatus());
            list.add(map);
        });
        Map<String,Object> map = new HashMap<>();
        map.put("data",list);
        map.put("total",roomPage.getTotal());
        return map;
    }

    /**
     * 小程序端会议室详情页
     * @return
     */
    @Override
    public RoomDetailVo getMeetRoomDetails(int id) {
        Room room = this.getById(id);
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        roomDetailVo.setId(room.getId());
        roomDetailVo.setStatus(room.getStatus());
        roomDetailVo.setImage(room.getImage());
        roomDetailVo.setLocation(room.getLocation());
        roomDetailVo.setDescription(room.getDescription());
        return roomDetailVo;
    }

    /**
     * 小程序端立即预约页面获取会议室数据
     * @return
     */
    @Override
    public List<Room> getMeetRoomData() {
        List<Room> roomList = this.list(new QueryWrapper<Room>().eq("status", CommonConstant.ROOM_STATUS_NOT_RESERVATION));

        return roomList;
    }
}
